package com.chalk.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.common.util.Result;
import com.chalk.config.base.BaseController;
import com.chalk.model.SysLookupType;
import com.chalk.service.SysLookupTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典(SysLookupType)表控制层
 *
 * @author maxiaoqiang
 * @since 2019-03-22 17:38:25
 */
@Api(value = "数据字典接口", tags = "数据字典模块")
@RestController
@RequestMapping("/v1/sysLookupType")
public class SysLookupTypeController extends BaseController {

    @Autowired
    private SysLookupTypeService sysLookupTypeService;

    /**
     * 数据字典列表
     *
     * @param current       当前页数
     * @param size          每页显示条数
     * @param sysLookupType 查询实体
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询数据字典列表", notes = "查询数据字典列表", response = Result.class)
    @GetMapping
    public Result selectAll(Integer current, Integer size, SysLookupType sysLookupType) {
        return Result.success(this.sysLookupTypeService.selectPage(this.getPage(current, size), new EntityWrapper<SysLookupType>(sysLookupType)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询数据字典", notes = "查询数据字典", response = Result.class)
    @GetMapping("{id}")
    public Result selectOne(
            @ApiParam(name = "id", value = "主键", required = true, example = "1")
            @PathVariable String id) {
        return Result.success(this.sysLookupTypeService.selectById(id));
    }


    /**
     * 保存数据字典
     *
     * @param sysLookupType 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "保存sysLookupType", notes = "保存sysLookupType", response = Result.class)
    @PostMapping
    public Result insert(
            @ApiParam(name = "sysLookupType", value = "传入json格式", required = true)
            @RequestBody SysLookupType sysLookupType) {
        DateTime date = DateUtil.date();
        sysLookupType.setCreateTime(date);
        sysLookupType.setUpdateTime(date);
        return Result.success(this.sysLookupTypeService.insert(sysLookupType));
    }

    /**
     * 修改数据字典
     *
     * @param sysLookupType 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "修改数据字典", notes = "修改数据字典", response = Result.class)
    @PutMapping
    public Result update(
            @ApiParam(name = "sysLookupType", value = "传入json格式", required = true)
            @RequestBody SysLookupType sysLookupType) {
        sysLookupType.setUpdateTime(DateUtil.date());
        return Result.success(this.sysLookupTypeService.updateById(sysLookupType));
    }

    /**
     * 逻辑删除数据字典
     *
     * @param ids 主键集合
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "批量删除数据字典", notes = "批量删除数据字典", response = Result.class)
    @DeleteMapping
    public Result delete(
            @ApiParam(name = "ids", value = "传数组", required = true)
            @RequestBody String[] ids) {
        return Result.success(this.sysLookupTypeService.deleteBatchByPrimaryKey(ids));
    }

    private Page<SysLookupType> getPage(Integer current, Integer size) {
        Page<SysLookupType> page = new Page<>();
        if (current != null) {
            page.setCurrent(current);
        }
        if (size != null) {
            page.setSize(size);
        }
        return page;
    }

}